//
//  StashViewController.swift
//  Box
//
//  Created by mn on 2025/3/28.
//
import UIKit
import SnapKit

class StashViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    private var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView.headerBeginRefreshing()
    }
    private func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 8
        layout.minimumLineSpacing = 8
        let totalPadding: CGFloat = 16 * 2 + 8 // 左右边距 + 1 个间距
        let itemWidth = (view.frame.width - totalPadding) / 2
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth * 0.75 + 80)
        layout.headerReferenceSize = CGSize(width: view.frame.width, height: view.frame.width * 209.0 / 375.0)

        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .fromHex("#efefef")
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.registerCells([StashCell.self])
        collectionView.register(StashHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: StashHeader.reuseIdentifier)
        view.addSubview(collectionView)

        collectionView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        collectionView.contentInset = .zero
        collectionView.addMJHeader {
            [weak self] in
            self?.page = 1
            self?.getMyStoreList()
        }
        collectionView.addMJFooter {
            [weak self] in
            guard let self else { return }
            page += 1
            getMyStoreList()
        }
        collectionView.headerBeginRefreshing()
    }
    var stashModels = [StashModel]()
    var page = 1
    func getMyStoreList() {
        APIService.request(api: .getMyStoreList) {
            [weak self] (result, response) in
            self?.collectionView.endAllRefreshing()
            if result.success, let models: StashModelData = Decoder.data(response) {
                if self?.page == 1 {
                    self?.stashModels.removeAll()
                }
                self?.stashModels.append(contentsOf: models.data)
                self?.collectionView.reloadData()
            }
        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        stashModels.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: StashCell = collectionView.cell(indexPath)
        cell.stashModel = stashModels[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        guard kind == UICollectionView.elementKindSectionHeader else {
            return UICollectionReusableView()
        }
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: StashHeader.reuseIdentifier, for: indexPath) as! StashHeader
        return header
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.width * 209.0 / 375.0)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
    }
}
